<?php
namespace app\api\controller\zfshop;
class Shopapply extends Base
{

    public function __construct() {
        parent::__construct();
        if(!$this->userinfo){
            if(is_post()){
                err('请先登录!',200);
            }
        }
    }

    //  店铺详情
    public function add(){
        $d = $this->post();
        $rule = [
            'title'=>'require',
            'name'=>'require',
            'phone'=>'require',
            'cat_title'=>'require',
            'address'=>'require',
        ];
        $msg = [];
        check($rule,$msg,$d);
        $info = $this->db->query("select id from shop_apply where user_id=?",[$this->userinfo["id"]],1);
        if($info){
            err("您已申请过，请勿重复申请");
        }
        $data = [
            'user_id'=>$this->userinfo['id'],
            'title'=>$d['title'],
            'name'=>$d['name'],
            'phone'=>$d['phone'],
            'cat_title'=>$d['cat_title'],
            'address'=>$d['address'],
            'create_time'=>time(),
        ];
        $rs = $this->db->insert("shop_apply",$data);
        if($rs){
            suc("操作成功");
        }else{
            err("操作失败");
        }
    }

    //查询申请
    public function details(){
        $info = $this->db->query("select * from shop_apply where user_id=?",[$this->userinfo["id"]],1);
        suc($info);
    }

    public function edit(){
        $d = $this->post();
        $rule = [
            'id'=>'require',
            'title'=>'require',
            'name'=>'require',
            'phone'=>'require',
            'cat_title'=>'require',
            'address'=>'require',
        ];
        $msg = [];
        check($rule,$msg,$d);
        $data = [
            'user_id'=>$this->userinfo['id'],
            'title'=>$d['title'],
            'name'=>$d['name'],
            'phone'=>$d['phone'],
            'cat_title'=>$d['cat_title'],
            'address'=>$d['address'],
            'create_time'=>time(),
        ];
        $rs = $this->db->update("shop_apply",$data,['id=? and user_id=?',[$d["id"],$this->userinfo["id"]]]);
        if($rs){
            suc("操作成功");
        }else{
            err("操作失败");
        }
    }

}
